using UnityEngine;
using System.Collections;

public class BulletBar : MonoBehaviour 
{
	PackedSprite[] bullets = new PackedSprite[4]; //, bullet2, bullet3, bullet4;
	int m_nCurrent = 0;
	int m_nMax = 4;
	void Awake()
	{
		bullets[0] = transform.GetChild(0).GetComponent<PackedSprite>();
		bullets[1] = transform.GetChild(1).GetComponent<PackedSprite>();
		bullets[2] = transform.GetChild(2).GetComponent<PackedSprite>();
		bullets[3] = transform.GetChild(3).GetComponent<PackedSprite>();
		
		/*Debug.Log(bullets[0]);
		Debug.Log(bullets[1]);
		Debug.Log(bullets[2]);
		Debug.Log(bullets[3]);
		*/
		bullets[0].Hide(true);
		bullets[1].Hide(true);
		bullets[2].Hide(true);
		bullets[3].Hide(true);
		
		/*bullet1 = transform.GetChild(0);
		bullet2 = transform.GetChild(1);
		bullet3 = transform.GetChild(2);
		bullet4 = transform.GetChild(3);*/
	}
	void OnEnable()
	{
	Messenger.AddListener("PickupAmmo", AddAmmo);
	Messenger.AddListener("RemoveAmmo", RemoveAmmo);	
	}
	void OnDisable()
	{
		
	Messenger.RemoveListener("PickupAmmo", AddAmmo);
	Messenger.RemoveListener("RemoveAmmo", RemoveAmmo);
	}
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}
	void AddAmmo()
	{
	
		if(m_nCurrent <= 3)
		{
	
			bullets[m_nCurrent].Hide(false);
			m_nCurrent++;
		}
		else
		{
			m_nCurrent = 3;
		}
		Debug.Log(m_nCurrent);
	}
	void RemoveAmmo()
	{	
		Debug.Log(m_nCurrent);
		m_nCurrent--;
		if(m_nCurrent >= 0)
		{
			bullets[m_nCurrent].Hide(true);
		}
		else
		{
			m_nCurrent = 0;
		}
		
	}
	public void SetCurrent(int _amount)
	{
		if(_amount < 4)
		{
			for(int i = 0; i < m_nMax; ++i)
			{
				if(i > _amount)
				{
					bullets[i].Hide(true);
					
				}
				if(bullets[i].IsHidden())
				{
					bullets[i].Hide(false);
				}
				
			}
		}
	}
}
